﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using DBHelper.Model;
namespace DBHelper.BLL
{
	/// <summary>
	/// News
	/// </summary>
	public partial class News
	{
		private readonly DBHelper.DAL.News dal=new DBHelper.DAL.News();
		public News()
		{}
		#region  Method

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(DBHelper.Model.News model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DBHelper.Model.News model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int NewsCode)
		{
			
			return dal.Delete(NewsCode);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string NewsCodelist )
		{
			return dal.DeleteList(NewsCodelist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DBHelper.Model.News GetModel(int NewsCode)
		{
			
			return dal.GetModel(NewsCode);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public DBHelper.Model.News GetModelByCache(int NewsCode)
		{
			
			string CacheKey = "NewsModel-" + NewsCode;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(NewsCode);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (DBHelper.Model.News)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<DBHelper.Model.News> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<DBHelper.Model.News> DataTableToList(DataTable dt)
		{
			List<DBHelper.Model.News> modelList = new List<DBHelper.Model.News>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				DBHelper.Model.News model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new DBHelper.Model.News();
					if(dt.Rows[n]["NewsCode"]!=null && dt.Rows[n]["NewsCode"].ToString()!="")
					{
						model.NewsCode=int.Parse(dt.Rows[n]["NewsCode"].ToString());
					}
					if(dt.Rows[n]["Title"]!=null && dt.Rows[n]["Title"].ToString()!="")
					{
					model.Title=dt.Rows[n]["Title"].ToString();
					}
					if(dt.Rows[n]["Description"]!=null && dt.Rows[n]["Description"].ToString()!="")
					{
					model.Description=dt.Rows[n]["Description"].ToString();
					}
					if(dt.Rows[n]["NewsContent"]!=null && dt.Rows[n]["NewsContent"].ToString()!="")
					{
					model.NewsContent=dt.Rows[n]["NewsContent"].ToString();
					}
					if(dt.Rows[n]["CreateTime"]!=null && dt.Rows[n]["CreateTime"].ToString()!="")
					{
						model.CreateTime=DateTime.Parse(dt.Rows[n]["CreateTime"].ToString());
					}
					if(dt.Rows[n]["Author"]!=null && dt.Rows[n]["Author"].ToString()!="")
					{
					model.Author=dt.Rows[n]["Author"].ToString();
					}
					if(dt.Rows[n]["PicPath"]!=null && dt.Rows[n]["PicPath"].ToString()!="")
					{
					model.PicPath=dt.Rows[n]["PicPath"].ToString();
					}
					if(dt.Rows[n]["VideoPath"]!=null && dt.Rows[n]["VideoPath"].ToString()!="")
					{
					model.VideoPath=dt.Rows[n]["VideoPath"].ToString();
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  Method
	}
}

